/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package rdfparser.bean;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import rdfparser.db.DBConnection;

/**
 *
 * @author Amendra Shrestha
 */
public class ForumBean {

    private int ForumID;
    private String messageBoard;
    private String type;
    private String link;
    private String title;
    private String description;
    private String parentForum;
    private String parentForumseeAlso;
    private String seeAlso;
    private List<ForumParent> postParentList;

    public ForumBean() {
        postParentList = new ArrayList<ForumParent>();
    }

    public List<ForumParent> getPostParentList() {
        return postParentList;
    }

    public int getForumID() {
        return ForumID;
    }

    public void setForumID(int ForumID) {
        this.ForumID = ForumID;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String getparentForum() {
        return parentForum;
    }

    public void setparentForum(String parentForum) {
        this.parentForum = parentForum;
    }

    public String getLink() {
        return link;
    }

    public void setLink(String link) {
        this.link = link;
    }

    public String getMessageBoard() {
        return messageBoard;
    }

    public void setMessageBoard(String messageBoard) {
        this.messageBoard = messageBoard;
    }

    public String getSeeAlso() {
        return seeAlso;
    }

    public void setSeeAlso(String seeAlso) {
        this.seeAlso = seeAlso;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getParentForumseeAlso() {
        return parentForumseeAlso;
    }

    public void setParentForumseeAlso(String parentForumseeAlso) {
        this.parentForumseeAlso = parentForumseeAlso;
    }

    public class ForumParent {

        private int ForumID;
        private int ForumThreadID;
        private String Thread;
        private String seeAlso;

        public int getForumID() {
            return ForumID;
        }

        public void setForumID(int ForumID) {
            this.ForumID = ForumID;
        }

        public int getForumThreadID() {
            return ForumThreadID;
        }

        public void setForumThreadID(int ForumThreadID) {
            this.ForumThreadID = ForumThreadID;
        }

        public String getThread() {
            return Thread;
        }

        public void setThread(String Thread) {
            this.Thread = Thread;
        }

        public String getSeeAlso() {
            return seeAlso;
        }

        public void setSeeAlso(String seeAlso) {
            this.seeAlso = seeAlso;
        }
    }

    public void addForum() {

        Connection con = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        String InsertForumSql = "INSERT INTO tbl_forum "
                + "(`Message_borad`, `Type`, `Link`, `Title`, "
                + "`Description`, `Parent`, `seeAlso`, `ParentseeAlso`) "
                + "values (?, ?, ?, ?, ?, ?, ?, ?)";
        try {
            con = DBConnection.getDBConnection();
            con.setAutoCommit(false);
            ps = con.prepareStatement(InsertForumSql);
            ps.setString(1, getMessageBoard());
            ps.setString(2, getType());
            ps.setString(3, getLink());
            ps.setString(4, getTitle());
            ps.setString(5, getDescription());
            ps.setString(6, getparentForum());
            ps.setString(7, getSeeAlso());
            ps.setString(8, getParentForumseeAlso());
            ps.executeUpdate();

            rs = con.prepareStatement("SELECT LAST_INSERT_ID()").executeQuery();
            if (rs.next()) {
                setForumID(rs.getInt(1));
            }
            con.commit();
        } catch (SQLException ex) {
            try {
                con.rollback();// rollback on error
            } catch (SQLException ex1) {
            }
            System.out.println("Error on adding data...." + ex.getMessage());
        } finally {
            try {
                if (con != null) {
                    con.close();
                }
                if (ps != null) {
                    ps.close();
                }
                if (rs != null) {
                    rs.close();
                }
            } catch (SQLException ex) {
            }
        }

    }

    public void addforumParent() {
        Connection con = null;
        PreparedStatement ps = null;
        try {
            String InsertForumParentSql = "INSERT INTO tbl_forum_thread "
                    + "(`parentThreadID`, `ForumID`, `Thread`, `threadseeAlso`)"
                    + "values (?, ?, ?, ?)";
            con = DBConnection.getDBConnection();
            for (ForumParent forum : getPostParentList()) {
                ps = con.prepareStatement(InsertForumParentSql);
                ps.setInt(1, forum.getForumThreadID());
                ps.setInt(2, getForumID());
                ps.setString(3, forum.getThread());
                ps.setString(4, forum.getSeeAlso());
                ps.executeUpdate();
            }
        } catch (SQLException ex) {
            System.err.println("Error while adding forum parents..." + ex.getMessage());
        } finally {
            try {
                if (con != null) {
                    con.close();
                }
                if (ps != null) {
                    ps.close();
                }
            } catch (SQLException ex) {
            }
        }
    }
}
